//
//  LandController3.m
//  quickLook
//
//  Created by J on 15/9/3.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "LandController3.h"
#import "AFHTTPRequestOperationManager.h"
#import "SetPassWordController.h"

@interface LandController3 ()
@property (weak, nonatomic) IBOutlet UITextField *registerCode;

@property (weak, nonatomic) IBOutlet UIButton *getRegisterCode;


@property (weak, nonatomic) IBOutlet UIButton *next;

@property(nonatomic,strong)UIAlertView* alertView;

@end

@implementation LandController3

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    self.navigationItem.title=@"填写验证码";
    
    _infoLabel.font=[UIFont systemFontOfSize:15];

//    _infoLabel.text=_phoneNumber;
    _next.layer.cornerRadius=3;
    

}
- (IBAction)getRegisterNumber:(id)sender {//发送验证码
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSDictionary *params = @{@"reason":@"register", @"phone":_phoneNumber};
    
    [manager POST:@"http://api.kuaikanmanhua.com/v1/phone/send_code" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        JZJLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        JZJLog(@"Error: %@", error);
    }];

    
    
    
    _getRegisterCode.backgroundColor=[UIColor lightGrayColor];

    
    
    
    
    
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [_getRegisterCode setTitle:@"发送验证码" forState:UIControlStateNormal];
                _getRegisterCode.backgroundColor=[UIColor orangeColor];

                _getRegisterCode.userInteractionEnabled = YES;
            });
        }else{
            //            int minutes = timeout / 60;
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [_getRegisterCode setTitle:[NSString stringWithFormat:@"%@秒后重新发送",strTime] forState:UIControlStateNormal];
                _getRegisterCode.userInteractionEnabled = NO;
               

                
            });
            timeout--;
            
        }
        

    });
    dispatch_resume(_timer);


    
}
- (IBAction)Next:(id)sender {
    
    if (_registerCode) {
        
        
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        NSDictionary *params = @{@"phone":_phoneNumber, @"code":_registerCode.text};
        JZJLog(@"%@",_registerCode);
        
        [manager POST:@"http://api.kuaikanmanhua.com/v1/phone/verify" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            JZJLog(@"%@",responseObject[@"message"]);
            
            
            if ([responseObject[@"message"] isEqualToString:@"invalid phone number"]) {
                
                
                [self Alert];
                
                
            }else{
            
            
                
                SetPassWordController* vc=[[SetPassWordController alloc]initWithNibName:@"SetPassWordController" bundle:nil];
                vc.navigationItem.title=@"设置密码";
                
                vc.phonNumber=_phoneNumber;
                [self.navigationController pushViewController:vc animated:YES];

            }
            
            JZJLog(@"JSON: %@", responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            JZJLog(@"Error: %@", error);
        }];
        
        
        

        
    }
    
    
    
    
}

- (void)Alert {
    _alertView = [[UIAlertView alloc] initWithTitle:nil
                                                                     message:@"验证码无效或者过期了"
                                                                    delegate:nil
                                                           cancelButtonTitle:nil otherButtonTitles:nil, nil];
                  
                  
                              [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                               target:self
                                                             selector:@selector(dismissAlertView:)
                                                             userInfo:nil
                                                              repeats:NO];
                  
                              [_alertView show];
    
                }


- (void)dismissAlertView:(NSTimer*)timer {
    
    [_alertView dismissWithClickedButtonIndex:0 animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
